#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
//
//bool validateStackSequences(int* pushed, int pushedSize, int* popped, int poppedSize) {
//    int* stack = (int*)malloc(sizeof(int) * pushedSize);
//    if (stack == NULL) {
//        return false;
//    }
//    int top = 0;
//    int j = 0;
//    for (int i = 0; i < pushedSize; ++i) {
//        stack[top] = pushed[i];
//        top++;
//        while (top > 0 && stack[top - 1] == popped[j]) {
//            top--;
//            j++;
//        }
//    }
//    bool result = (top == 0);
//    free(stack);
//    return result;
//}
//
//int main() {
//    int pu[5] = { 1, 2, 3, 4, 5 };
//    int po[5] = { 4, 5, 3, 2, 1 };
//    if (validateStackSequences(pu, 5, po, 5)) {
//        return 1;
//    }
//    return 0;
//}
//#include<stdio.h>
bool  validateStackSequences(int* pushed, int pushedSize, int* popped, int poppedSize) {
    int* stack = (int*)malloc(sizeof(int) * pushedSize);
    if (stack == NULL) {
        return false;
    }
    int top = 0;
    int j = 0;
    for (int i=0; i < pushedSize; ++i, ++top) {
        stack[top] = pushed[i];
        while (top >= 0 && stack[top] == popped[j]) {
            top--;
            j++;
        }
    }
    free(stack);
    return top==0;

}
int  main() {
    int pu[5] = { 1, 2, 3, 4, 5 };
    int po[5] = { 4,5,3,2,1 };
    if (validateStackSequences(pu, 5, po, 5)) {
        return 1;
    }
    return 0;
}